/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#ifndef INCLUDE_SHADER_PBR_FUNCTIONS
#define INCLUDE_SHADER_PBR_FUNCTIONS


#include "shader_binding_ID.h"
#include "shader_built_in_functions.h"
#include "shader_std.h"
#include "shader_ray.h"


#ifdef __cplusplus

//#include <基本类型.h>

using uint = unsigned int;

#define in
#define out
#define inout

#else

#define Inline

vec3 f_MaterialPBR_calc(in S_Material material, in vec3 RayDirection, in S_SceneDataCall hitValue) {
	float metallic = 0.8;
	//float roughness = material.m_roughness;
	float roughness = 0.5;
	vec3 F0 = vec3(0.04);
	//return F0;
	//vec3 albedo = (material.m_color) * hitValue.irradiance;
	//vec3 albedo = vec3(material.m_color_r, material.m_color_g, material.m_color_b);
	//vec3 albedo = vec3(1);
	//albedo = vec3(0, 0, 0);
	//return vec3(material.m_color);
	vec3 albedo = material.m_color;// * hitValue.irradiance;

	vec3 V = RayDirection;
	vec3 H = normalize(V - hitValue.ligthDir);

	//float denom = 4 * max(dot(hitValue.Normal, V), 0) * max(dot(hitValue.Normal, LigthDir), 0.0) + 0.0001;
	float NDF = f_DistributionGGX(hitValue.Normal, H, roughness);
	//return albedo * NDF;


	F0 = mix(F0, albedo, metallic);
	vec3 F = f_fresnelSchlick(max(dot(H, V), 0), F0);
	
	vec3 KD = vec3(1) - F;
	KD *= 1.0 - metallic;
	
	float G = f_GeometrySmith(hitValue.Normal, (V), hitValue.ligthDir, roughness);
	//return vec3(F);
	
	vec3 numerator = NDF * F * G;
	
	
	//float denominator = float(hitValue.Depth) * max(dot(hitValue.Normal, V), 0.0) * max(dot(hitValue.Normal, LigthDir), 0.0) + 0.0001;
	float denominator = 1.0 * max(dot(hitValue.Normal, V), 0.0) * max(dot(hitValue.Normal, hitValue.ligthDir), 0.0) + 0.0001;
	vec3 specular = numerator / denominator;
	//hitValue.Color = vec3(hitValue.irradiance);
	//return vec3(specular);

	float NdotL = max(dot(hitValue.Normal, hitValue.ligthDir), 0);
	vec3 Color = (KD * albedo / M_PI + specular) * NdotL;
	
	Color = F0 * albedo + Color;// * hitValue.irradiance;
	//Color = albedo + Color;// *hitValue.irradiance; * hitValue.irradiance
	//Color = material.m_color;
	//return vec3(1,0,0);
	//material.m_roughness = NDF;
	return Color;
}



#endif


//���߷ֲ���
Inline float f_SpecularDistribution(in vec3 N, in vec3 H, float roughness) {
    float a2 = roughness * roughness;
    float NdotH = max(dot(N, H), 0.0);
    float NdotH2 = NdotH * NdotH;

    float nom = a2;
    float denom = (NdotH2 * (a2 - 1.0) + 1.0);
    denom = M_PI * denom * denom;

    return nom / denom;
}




#endif

